import { defineStore } from "pinia";
import { templateSelectDic } from "@/utils/dicts";
import { RdTypeEnum, TemplateType, WorkFlowTypeEnum } from "@/utils/enums";
import { type TemplateChooseObjStore, type TemplateChooseObjItem } from "../indexType";

import BuildSvg from "@/static/svg/build.svg";
import BuildActSvg from "@/static/svg/build-light.svg";
import RoomSvg from "@/static/svg/room.svg";
import RoomActSvg from "@/static/svg/room-light.svg";
import LandscapeSvg from "@/static/svg/landscape.svg";
import LandscapeActSvg from "@/static/svg/landscape-light.svg";
import PlanSvg from "@/static/svg/plan.svg";
import PlanActSvg from "@/static/svg/plan-light.svg";

const defaultTemplates = [
  {
    title: "建筑",
    icon: BuildSvg,
    iconAct: BuildActSvg,
    key: TemplateType.BUILD,
  },
  {
    title: "室内",
    icon: RoomSvg,
    iconAct: RoomActSvg,
    key: TemplateType.ROOM,
  },
  {
    title: "景观",
    icon: LandscapeSvg,
    iconAct: LandscapeActSvg,
    key: TemplateType.LANDSCAPE,
  },
  {
    title: "规划",
    icon: PlanSvg,
    iconAct: PlanActSvg,
    key: TemplateType.PLAN,
  },
];

const defaultRdOptions = [
  {
    label: "精细",
    value: RdTypeEnum.DETAILED,
    dec: "线条丰富，主体轮廓明确，带有部分细节(可带阴影或颜色)的线稿",
  },
  {
    label: "粗略",
    value: RdTypeEnum.ROUGH,
    dec: "线条简单，主体轮廓抽象的线稿，AI会增加自身理解(如与意象差距较大，建议增加线稿细节，如增加天空、地面等背景的线条)",
  },
];

const defaultImageParameters = {
  rdType: RdTypeEnum.DETAILED, // 模型粒度
  styleStrength: {
    value: 0.5,
  }, // 风格图风格指数
  similarity: 0.3, // 灵感迁移相似度
  originStrength: {
    // 原图指数
    checked: false,
    value: 0.1,
    min: 0,
    max: 0.5,
  },
};

export type ImageParametersType = typeof defaultImageParameters;

const defaultBatchSize = 1;

export const useWorkflowStore = defineStore("workflow", () => {
  // 工作流类型
  const workflowType = ref<WorkFlowTypeEnum>(WorkFlowTypeEnum.PICTURE_CREATE_BY_LINE);

  /* 模板数据 */
  // 模板选择对象
  let templateChooseObj = ref<TemplateChooseObjStore>({
    currentTemplate: {
      label: "建筑",
      value: TemplateType.BUILD, // 模板类型
    },
    businessOption: {
      label: templateSelectDic[1].type[0].title,
      value: templateSelectDic[1].type[0].key, // 业态
    },
    subdivision: {
      label: templateSelectDic[1].type[0].list[0].title,
      value: templateSelectDic[1].type[0].list[0].key, // 细分类型
    },
  });
  // 模板数组
  const templateStyles = computed(() => {
    let templates: typeof defaultTemplates = [];
    switch (workflowType.value) {
      case WorkFlowTypeEnum.OLD_BUILDING_REBUILD:
        templates = [
          {
            title: "建筑",
            icon: BuildSvg,
            iconAct: BuildActSvg,
            key: TemplateType.BUILD,
          },
        ];
        break;
      case WorkFlowTypeEnum.ROOM_REBUILD:
        templates = [
          {
            title: "室内",
            icon: RoomSvg,
            iconAct: RoomActSvg,
            key: TemplateType.ROOM,
          },
        ];
        break;
      default:
        templates = defaultTemplates;
        break;
    }

    templateChooseObj.value = {
      currentTemplate: {
        label: templates[0].title,
        value: templates[0].key, // 模板类型
      },
      businessOption: {
        label: templateSelectDic[templates[0].key].type[0].title,
        value: templateSelectDic[templates[0].key].type[0].key, // 业态
      },
      subdivision: {
        label: templateSelectDic[templates[0].key].type[0].list[0].title,
        value: templateSelectDic[templates[0].key].type[0].list[0].key, // 细分类型
      },
    };
    return templates;
  });

  /* 图像数据 */
  const imageObj = reactive({
    baseImgUrl: "",
    styleImgUrl: "",
  });
  //  图片参数相关
  const imageParameters = ref<ImageParametersType>(defaultImageParameters);
  const rdTitle = ref("");
  const rdOptions = computed(() => {
    let resOptions: typeof defaultRdOptions = [];
    switch (workflowType.value) {
      case WorkFlowTypeEnum.PICTURE_CREATE_BY_LINE:
        resOptions = defaultRdOptions;
        rdTitle.value = "我的线稿是：";
        break;
      case WorkFlowTypeEnum.MODEL_RENDER:
        resOptions = [
          {
            label: "精细",
            value: RdTypeEnum.DETAILED,
            dec: "可区分背景，主体结构清晰，带有部分细节(可带材质贴图与颜色)的模型",
          },
          {
            label: "粗略",
            value: RdTypeEnum.ROUGH,
            dec: "无法区分背景，仅有简单体块的白模，AI会增加自身理解(如与意象差距较大，建议增加建模细节，如区分天空、地面等背景的颜色)",
          },
        ];
        rdTitle.value = "我的模型是：";
        break;
      case WorkFlowTypeEnum.ROOM_REBUILD:
        resOptions = [
          {
            label: "带家具",
            value: RdTypeEnum.DETAILED,
            dec: "对带家具等软装与饰面等硬装的装修房，改造装修风格",
          },
          {
            label: "毛坯房",
            value: RdTypeEnum.ROUGH,
            dec: "对不带家具的毛坯房或硬装房，进行装修并生成家具",
          },
        ];
        rdTitle.value = "我的房间是：";
        break;
      default:
        resOptions = [];
        rdTitle.value = "";
    }
    return resOptions;
  });

  // 提示词
  const cueWordObj = ref({
    prompt: "",
    negativePrompt: "",
    promptOptimation: false,
  });

  // 生成数量
  const batchSize = ref<number>(defaultBatchSize);
  // 修改工作流类型
  const setWorkflowType = (type: WorkFlowTypeEnum) => {
    workflowType.value = type;
    resetState();
    if (type === WorkFlowTypeEnum.OLD_BUILDING_REBUILD || type === WorkFlowTypeEnum.ROOM_REBUILD) {
      imageParameters.value.originStrength.checked = true;
    }
  };
  // 修改模板选择对象
  const setTemplateChooseObj = <T>(value: T, key?: keyof typeof templateChooseObj.value) => {
    if (!key) {
      templateChooseObj.value = value as TemplateChooseObjStore;
      return;
    }
    if (key === "currentTemplate") {
      templateChooseObj.value[key].value = value as number;
    } else {
      templateChooseObj.value[key] = value as TemplateChooseObjItem;
    }
  };

  // 重置状态
  const resetState = () => {
    imageParameters.value = JSON.parse(JSON.stringify(defaultImageParameters));
    imageObj.baseImgUrl = "";
    imageObj.styleImgUrl = "";
    batchSize.value = defaultBatchSize;
    cueWordObj.value = {
      prompt: "",
      negativePrompt: "",
      promptOptimation: false,
    };
  };

  // 初始化数据用于返现
  // const loadDesignData =()=> {}
  return {
    workflowType,
    templateChooseObj,
    templateStyles,
    imageObj,
    imageParameters,
    rdTitle,
    rdOptions,
    batchSize,
    cueWordObj,
    setWorkflowType,
    setTemplateChooseObj,
    resetState,
  };
});
